package one.bugu.android.demon.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.ExchangeBean;
/**
 * 兑换服务
 */
public class ExchangeServiceAdapter extends BaseAdapter {
    private List<ExchangeBean.DataBean> mList = new ArrayList<>();
    private Context mContext;

    public ExchangeServiceAdapter(Context context, List<ExchangeBean.DataBean> list) {
        super();
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public ExchangeBean.DataBean getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolderOne holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_exchange, parent, false);
            holder = new ChildHolderOne();
            convertView.setTag(holder);
        } else {
            holder = (ChildHolderOne) convertView.getTag();
        }
        holder.tvTitle = convertView.findViewById(R.id.item_title);
        holder.tvTitle1 = convertView.findViewById(R.id.item_title1);
        holder.tvTitle2 = convertView.findViewById(R.id.item_title2);
        holder.myl = convertView.findViewById(R.id.ly_bg);

        final ExchangeBean.DataBean gridInfo = mList.get(position);
        if (gridInfo.getUseNum() > gridInfo.getUseNumFact()) {
            holder.tvTitle2.setText(String.valueOf(gridInfo.getUseNumFact()) + gridInfo.getUseCoinName());
        } else {
            holder.tvTitle2.setText(String.valueOf(gridInfo.getUseNum()) + gridInfo.getUseCoinName());
        }
        holder.tvTitle.setText(gridInfo.getGoodsName());
        String desc = "";
        if (!TextUtils.isEmpty(gridInfo.getConvertDesc())) {
            desc = gridInfo.getConvertDesc().replace(",", "+");
        }
        holder.tvTitle1.setText(desc);
        holder.tvTitle2.setText(String.valueOf(gridInfo.getUseNumFact()) + gridInfo.getUseCoinName());
        if (!gridInfo.isLimitGoods()) {
            holder.myl.setBackgroundResource(R.drawable.shape_good_bg);
        } else {
            if (gridInfo.isSelect()) {
                holder.myl.setBackgroundResource(R.mipmap.duihuan_xuanzhong);
                holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.colorWhite));
                holder.tvTitle1.setTextColor(mContext.getResources().getColor(R.color.colorWhite));
                holder.tvTitle2.setTextColor(mContext.getResources().getColor(R.color.colorWhite));
            } else {
                holder.myl.setBackgroundResource(R.drawable.shape_white_bg);
                holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.color_333));
                holder.tvTitle1.setTextColor(mContext.getResources().getColor(R.color.color_BBBBBB));
                holder.tvTitle2.setTextColor(mContext.getResources().getColor(R.color.color_999999));
            }
        }
        return convertView;
    }

    class ChildHolderOne {
        TextView tvTitle;
        TextView tvTitle1;
        TextView tvTitle2;
        LinearLayout myl;
    }

    /**
     * 获取列表数据
     */
    public List<ExchangeBean.DataBean> getAllData() {
        return mList;
    }

    /**
     * 刷新UI
     */
    public void notifyList(List<ExchangeBean.DataBean> mList) {
        if (null != mList && mList.size() > 0) {
            this.mList = mList;
            notifyDataSetChanged();
        }
    }
}
